package com.inno.subject.controller.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.inno.subject.common.annotiation.Log;
import com.inno.subject.common.context.LoginUserContext;
import com.inno.subject.common.entity.CommonReturnType;
import com.inno.subject.common.error.BusinessException;
import com.inno.subject.controller.convert.InterviewDTOConverter;
import com.inno.subject.controller.dto.InterviewDTO;
import com.inno.subject.controller.dto.ResumeDiagnoseDTO;
import com.inno.subject.domain.entity.InterviewBO;
import com.inno.subject.domain.entity.ResumeDiagnoseBO;
import com.inno.subject.domain.service.InterviewDomainService;
import com.inno.subject.domain.utils.CommonUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author sean
 */
@Slf4j
@RestController
@RequestMapping("/subject/interview")
public class InterviewController {
    @Autowired
    private InterviewDomainService interviewDomainService;

    @PostMapping("/mock")
    @Log(operationType = "模拟面试")
    public CommonReturnType<Void> getInterview(@RequestBody InterviewDTO interviewDTO, HttpServletResponse response) {
        interviewDTO.setCurrentUser(LoginUserContext.getUserId());
        InterviewBO interviewBO = InterviewDTOConverter.INSTANCE.convertDTOToBO(interviewDTO);
        interviewDomainService.mockInterview(interviewBO, response);
        return CommonReturnType.success();
    }

    /**
     * 上传简历并解析
     */
    @PostMapping("/upload")
    @Log(operationType = "模拟面试-解析简历")
    public CommonReturnType<String> uploadAndParse(@RequestParam("file") MultipartFile file, @RequestParam("type") String type) {
        Long currentUser = LoginUserContext.getUserId();
        String fileUrl = interviewDomainService.uploadAndParse(file, type, currentUser);
        return CommonReturnType.success(fileUrl);
    }

    /**
     * 简历诊断
     */
    @PostMapping("/diagnose")
    @Log(operationType = "简历诊断")
    public CommonReturnType<ResumeDiagnoseDTO> diagnose(@RequestParam("file") MultipartFile file,
                                                        @RequestParam("workAgeLimit") String workAgeLimit,
                                                        @RequestParam("desiredPosition") String desiredPosition) {
        if (log.isInfoEnabled()){
            log.info("InterviewController.diagnose.file: {}", file);
            log.info("InterviewController.diagnose.workAgeLimit: {}", workAgeLimit);
            log.info("InterviewController.diagnose.desiredPosition: {}", desiredPosition);

        }
        Assert.notNull(file, () -> new BusinessException("文件不能为空"));
        if (!CommonUtil.isPdfOrWord(file.getOriginalFilename())) {
            throw new BusinessException("请上传pdf或word文件");
        }
        Assert.notBlank(workAgeLimit, () -> new BusinessException("请选择您的工作年限"));
        Assert.notBlank(desiredPosition, () -> new BusinessException("请选择期望职位"));
        Long currentUser = LoginUserContext.getUserId();
        ResumeDiagnoseDTO resumeDiagnoseDTO = new ResumeDiagnoseDTO();
        resumeDiagnoseDTO.setCurrentUser(currentUser);
        resumeDiagnoseDTO.setFile(file);
        resumeDiagnoseDTO.setWorkAgeLimit(workAgeLimit);
        resumeDiagnoseDTO.setDesiredPosition(desiredPosition);
        ResumeDiagnoseBO boResult = this.interviewDomainService.diagnose(convertDTOToBO(resumeDiagnoseDTO));
        return CommonReturnType.success(convertBOToDTO(boResult));

    }

    private static ResumeDiagnoseDTO convertBOToDTO(ResumeDiagnoseBO bo) {
        ResumeDiagnoseDTO resumeDiagnoseDTO = new ResumeDiagnoseDTO();
        BeanUtil.copyProperties(bo, resumeDiagnoseDTO);
        return resumeDiagnoseDTO;
    }

    private static ResumeDiagnoseBO convertDTOToBO(ResumeDiagnoseDTO dto) {
        ResumeDiagnoseBO bo = new ResumeDiagnoseBO();
        BeanUtil.copyProperties(dto, bo);
        return bo;
    }
}
